<?php

namespace App\Http\Controllers\V1\Admin;


use App\Models\V1\Position;
use Illuminate\Http\Request;

class PositionController extends BaseController
{

    /**
     * 工种列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $query=Position::query();
        $pageSize=$request->input('pageSize',10);
        $order=$request->input('order','desc');
        $sort=$request->input('sort','id');
        if($request->filled('name')){
            $query->where('name','like',"%".$request->input('name',"")."%");
        }
        if($request->filled('position_class_id')){
            $query->where('position_class_id',$request->input('position_class_id',0));
        }
        if($request->filled('phone')){
            $query->where('phone',$request->input('phone'));
        }
        if($request->filled('state')){
            $query->where('state',$request->input('state',0));
        }

        $query->orderBy($sort,$order);
        $data=$query->paginate($pageSize);
        return ajax_output(200,"成功",$data);
    }


    /**
     * 新增工种
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $model=new Position();
        $model->create([
            'position_class_id'=>$request->input('position_class_id'),
            'position_name'=>$request->input('position_name',""),
            'name'=>$request->input('name',""),
            'phone'=>$request->input('phone'),
            'thumb'=>$request->input('thumb',""),
            'particulars'=>$request->input('particulars'),
            'state'=>$request->input('state',1),
            'birth'=>$request->input('birth'),
            'skill'=>$request->input('skill'),
            'work'=>$request->input('work'),
            'c_num'=>$request->input('c_num'),
            'price'=>$request->input('price',""),
            'price_unit'=>$request->input('price_unit',""),
            'province_id'=>$request->input('province_id'),
            'province_name'=>$request->input('province_name'),
            'city_id'=>$request->input('city_id'),
            'city_name'=>$request->input('city_name'),
            'district_id'=>$request->input('district_id'),
            'district_name'=>$request->input('district_name'),
            'address'=>$request->input('address'),
            'show'=>$request->input('show'),
            'recommend'=>$request->input('recommend'),


        ]);
        return ajax_output(200,"成功");
    }

    /**
     * 工种详情
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $vo=Position::find($id);
        return ajax_output(200,"成功",$vo);
    }


    /**
     * 更新工种
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $vo=Position::find($id);
        $vo->update([
            'position_class_id'=>$request->input('position_class_id'),
            'position_name'=>$request->input('position_name',""),
            'name'=>$request->input('name',""),
            'phone'=>$request->input('phone'),
            'thumb'=>$request->input('thumb',""),
            'particulars'=>$request->input('particulars'),
            'state'=>$request->input('state',1),
            'birth'=>$request->input('birth'),
            'skill'=>$request->input('skill'),
            'work'=>$request->input('work'),
            'c_num'=>$request->input('c_num'),
            'price'=>$request->input('price',""),
            'price_unit'=>$request->input('price_unit',""),
            'province_id'=>$request->input('province_id'),
            'province_name'=>$request->input('province_name'),
            'city_id'=>$request->input('city_id'),
            'city_name'=>$request->input('city_name'),
            'district_id'=>$request->input('district_id'),
            'district_name'=>$request->input('district_name'),
            'address'=>$request->input('address'),
            'show'=>$request->input('show'),
            'recommend'=>$request->input('recommend'),
        ]);
        return ajax_output(200,"成功");
    }

    /**
     * 删除工种
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $id = explode(',',$id);
        Position::destroy($id);
        return ajax_output(200,"成功");
    }
}
